package com.fc.app.fcapp.pages.linechart;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.fc.app.fcapp.CoreFuncKt;
import com.fc.app.fcapp.R;
import com.fc.library.fresco.Util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.concurrent.CountDownLatch;

import rx.functions.Func0;


/**
 * Created by Administrator on 2017/12/6.
 */

public class ChartActivity extends Activity {
    private ChartMainLayout mainLayout;
    private WebView contentWebView;
    private ImageView progressbar_bg,progressbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mainLayout = new ChartMainLayout(getApplicationContext());

        setContentView(R.layout.java_js);

       /* FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(Util.Div(42), Util.Div(338));
        ImageView imageView = new ImageView(this);
        params.leftMargin = Util.Div(400);
        params.topMargin = Util.Div(400);
        imageView.setBackgroundResource(R.drawable.as_net_speed_manager_pointer);
        mainLayout.addView(imageView,params);

        imageView.setPivotX(0);
        imageView.setPivotY(0);
        Log.i("UUU","  "+Util.Div(-119));

        ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "rotation", 0, 360);
        animator.setDuration(10000);
        animator.start();
        setContentView(mainLayout);

        Util.instence(this);
        FrameLayout progressLayout = new FrameLayout(this);
        progressLayout.setBackgroundColor(Color.GRAY);
        params = new FrameLayout.LayoutParams(Util.Div(258 ), Util.Div(36 ));
        params.topMargin = Util.Div(590 - 300);
        params.gravity = Gravity.CENTER_HORIZONTAL;
        mainLayout.addView(progressLayout, params);
        
        progressbar_bg = new ImageView(this);
        params = new FrameLayout.LayoutParams(Util.Div(258), Util.Div(36));
        params.gravity = Gravity.CENTER;
        progressbar_bg.setBackgroundResource(R.drawable.as_net_speed_manager_progressbar_bg);
        progressLayout.addView(progressbar_bg, params);

        progressbar = new ImageView(this);
        params = new FrameLayout.LayoutParams(Util.Div(258), Util.Div(36));
        params.gravity =Gravity.CENTER;
        progressbar.setBackgroundResource(R.drawable.as_net_speed_manager_progressbar);
        progressLayout.addView(progressbar, params);


        progressbar.setPivotY(-Util.Div(264));
        progressbar.setPivotX(Util.Div(129));
//        progressbar.setPivotY(-Util.Div(0));
//        progressbar.setPivotX(Util.Div(0));


//        停在左侧看不见区域
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(progressbar, "rotation", 0, 51.43f);
        animator.setDuration(0);
        animator1.start();

        BlueButton bu1 = new BlueButton(this,"111",100,100);
        params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.topMargin = Util.Div(400);
        params.leftMargin = Util.Div(400);
        mainLayout.addView(bu1,params);

        BlueButton bu2 = new BlueButton(this,"222",100,100);
        params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.topMargin = Util.Div(600);
        params.leftMargin = Util.Div(400);
        mainLayout.addView(bu2,params);

        Button bu3 = new Button(this);
        params = new FrameLayout.LayoutParams(Util.Div(100),Util.Div(100));
        params.topMargin = Util.Div(700);
        params.leftMargin = Util.Div(400);
        mainLayout.addView(bu3,params);




        bu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"xxxxxx",Toast.LENGTH_LONG).show();
            }
        });*/


        contentWebView = (WebView) findViewById(R.id.webview);
        // 启用javascript
        contentWebView.getSettings().setJavaScriptEnabled(true);
        contentWebView.loadUrl("file:///android_asset/web.html");
        contentWebView.addJavascriptInterface(ChartActivity.this,"android");

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contentWebView.loadUrl("javascript:javacalljs()");
            }
        });

        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contentWebView.loadUrl("javascript:javacalljswith("+ "'JAVA调用了JS的有参函数'" + ")");
            }
        });




//        startProgressBarAnim();


    }


    private void startProgressBarAnim() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(progressbar, "rotation",  51.43f,0);
//        ObjectAnimator animator = ObjectAnimator.ofFloat(progressbar, "rotation",0);
        animator.setDuration(19000);
        animator.start();
    }
    @JavascriptInterface
    public void startFunction(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(ChartActivity.this,"js调用java代码（无参）",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @JavascriptInterface
    public void startFunction(final String text){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new AlertDialog.Builder(ChartActivity.this).setMessage(text).show();
            }
        });
    }

}
